package com.iogogogo.supervisord4j.api;

import com.iogogogo.supervisord.base.BaseResult;
import com.iogogogo.supervisord.base.ResponseWrapper;
import com.iogogogo.supervisord.exception.SupervisordException;
import com.iogogogo.supervisord4j.pojo.request.JobInfoRequest;
import com.iogogogo.supervisord4j.pojo.request.ReadLogRequest;
import com.iogogogo.supervisord4j.service.SupervisordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * Created by tao.zeng on 2021/7/7.
 */
@Api(value = "supervisord4j", tags = "Supervisord Api")
@RestController
@RequestMapping("/api/supervisord/{instance}")
public class SupervisordApi implements BaseResult {

    private final SupervisordService supervisordService;

    public SupervisordApi(SupervisordService supervisordService) {
        this.supervisordService = supervisordService;
    }

    @ApiOperation(value = "获取supervisord所有运行进程")
    @GetMapping("/{pageNo}/{pageSize}")
    public ResponseWrapper<?> list(@PathVariable("instance") String instance,
                                   @PathVariable("pageNo") int pageNo,
                                   @PathVariable("pageSize") int pageSize,
                                   @RequestParam(required = false, value = "condition") String condition) throws SupervisordException, IOException {

        return ok(supervisordService.partition(instance, pageNo, pageSize, condition));
    }

    @ApiOperation(value = "修改job扩展信息")
    @RequestMapping(value = "/{jobId}", method = {RequestMethod.PUT})
    public ResponseWrapper<?> update(@PathVariable("instance") String instance,
                                     @PathVariable("jobId") Long jobId, @RequestBody JobInfoRequest request) {
        return ok(supervisordService.updateJob(instance, jobId, request));
    }

    @ApiOperation(value = "启停任务")
    @PostMapping("/{processName}/{action}")
    public ResponseWrapper<?> status(@PathVariable("instance") String instance,
                                     @PathVariable("processName") String processName,
                                     @PathVariable("action") String action,
                                     @RequestParam(required = false, defaultValue = "false") Boolean waitToStop) throws SupervisordException, IOException {
        return ok(supervisordService.status(instance, processName, action, waitToStop));
    }

    @ApiOperation(value = "启停所有任务")
    @PostMapping("/job/{action}")
    public ResponseWrapper<?> jobAction(@PathVariable("instance") String instance,
                                        @PathVariable("action") String action,
                                        @RequestParam(required = false, defaultValue = "false") Boolean wait) throws SupervisordException, IOException {
        return ok(supervisordService.allProcessStatus(action, instance, wait));
    }

    @ApiOperation(value = "read进程stdout/stderr日志")
    @PostMapping("read-process-log")
    public ResponseWrapper<?> readProcessLog(@PathVariable("instance") String instance,
                                             @RequestParam(value = "logType", defaultValue = "stdout") String logType,
                                             @RequestBody ReadLogRequest request) throws SupervisordException, IOException {
        switch (logType) {
            case SupervisordService.LOG_STDERR:
                return ok(supervisordService.readProcessStderrLog(instance, request));
            case SupervisordService.LOG_STDOUT:
            default:
                return ok(supervisordService.readProcessStdoutLog(instance, request));
        }
    }

    @ApiOperation(value = "tail进程stdout/stderr日志")
    @PostMapping("tail-process-log")
    public ResponseWrapper<?> tailProcessLog(@PathVariable("instance") String instance,
                                             @RequestParam(value = "logType", defaultValue = "stdout") String logType,
                                             @RequestBody ReadLogRequest request) throws SupervisordException, IOException {
        switch (logType) {
            case SupervisordService.LOG_STDERR:
                return ok(supervisordService.tailProcessStderrLog(instance, request));
            case SupervisordService.LOG_STDOUT:
            default:
                return ok(supervisordService.tailProcessStdoutLog(instance, request));
        }
    }

    @ApiOperation(value = "清理进程日志")
    @PostMapping("clear-process-log/{processName}")
    public ResponseWrapper<?> clearProcessLog(@PathVariable("instance") String instance,
                                              @PathVariable("processName") String processName) throws SupervisordException, IOException {
        return ok(supervisordService.clearProcessLog(instance, processName));
    }

    @ApiOperation(value = "清理所有日志")
    @PostMapping("clear-log")
    public ResponseWrapper<?> clearLog(@PathVariable("instance") String instance) throws SupervisordException, IOException {
        return ok(supervisordService.clearLog(instance));
    }
}
